# coding = utf-8
import multiprocessing
import sys
import unittest
import HwTestReport
sys.path.append("D:/test/Appium2021")
from util.write_user_command import WriteUserCommand
from util.server import Server
from business.main_business import MainBusiness
from base.basic import Basic
from time import sleep
class ParameTestCase(unittest.TestCase):
    def __init__(self,methodName='runTest',param=None):
        super(ParameTestCase,self).__init__(methodName)
        self.param = param
        global params
        params = param
class Demo2(ParameTestCase):
    @classmethod
    def setUpClass(cls):
        global params
        cls.main_businees = MainBusiness(params)
        cls.driver = cls.main_businees.main_handle.main_page.driver
        sleep(10)

    def setUp(self):
        base = Basic(self.driver)
        self.imgs = base.imgs()
        self.add_imgs = base.add_imgs()
    def get_screenshot(self):
        self.imgs.append(self.driver)
        return True


    # @unittest.skip("test_01") # 跳过test_01用例
    def test_01(self):
        self.main_businees.to_my()
    def test_02(self):
        print("case02")
        # self.assertEqual("1", "2","数据错误")

    def tearDown(self):
        print("teardown")
        if sys.exc_info()[0]:
            print(sys.exc_info()[0])
            self.add_imgs()
    @classmethod
    def tearDownClass(cls):
        server = Server()
        server.kill_server()
        print("cls down")



def appium_init():
    server = Server()
    server.main()
    # time.sleep(20)
def get_count():
    write_user_file = WriteUserCommand()
    count = write_user_file.get_file_lines()
    return count



def get_suite(i):
    # 申请一个容器
    suite = unittest.TestSuite()
    # 添加要运行的用例
    # suite.addTest(CaseTest("test_02"))
    suite.addTest(Demo2("test_01", param=i))
    html_file ="../report/Report"+str(i)+".html"
    # 运行容器
    with open(html_file, 'wb') as report:
        runner = HwTestReport.HTMLTestReport(stream=report,
                                             verbosity=2,
                                             title='HwTestReport 测试',
                                             description='带截图，带饼图，带详情',
                                             tester='lee')
        runner.run(suite)

if __name__ == '__main__':
    appium_init()
    # count = get_count()
    # get_suite_one()
    threads = []
    for i in range(get_count()):
        t = multiprocessing.Process(target=get_suite, args=(i,))
        threads.append(t)
    for j in threads:
        j.start()

